package marchsoft.modules.selfFish.pisciculture.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import marchsoft.base.PageVO;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.selfFish.pisciculture.entity.dto.EquipmentDto;
import marchsoft.modules.selfFish.pisciculture.service.EquipmentService;
import marchsoft.modules.system.entity.dto.JobQueryCriteria;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**设备管理
 * @title FishController
 * @description 设备管理
 * @author Lenovo
 * @version 1.0
 * @create 2024-07-15 16:53
 */
@Slf4j
@RestController
@RequestMapping("/api/equipment")
@Api(tags = "养鱼管理：设备管理")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    private static final String ENTITY_NAME = "equipment";

    @GetMapping
    @ApiOperation("获取设备列表")
    @PreAuthorize("@smpe.check('equipment:list')")
    public Result<Object> query(String equipmentName,
                                @RequestParam(value ="pageNum",defaultValue = "1")Long pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "10") Long pageSize ){
        log.info(StrUtil.format("【获取设备列表 /api/equipment】操作人id：{}，设备查询条件 equipmentName：{}，分页pageNum：{}", SecurityUtils.getCurrentUserId(),equipmentName, pageNum));
        return equipmentService.getEquipmentList(equipmentName,pageNum,pageSize);
    }

    @PostMapping
    @ApiOperation("新增设备")
    @PreAuthorize("@smpe.check('equipment:add')")
    public Result<Object> create(@RequestBody EquipmentDto equipmentDto){
        log.info(StrUtil.format("【新增设备 /api/equipment】操作人id：{}，新增设备实体：{}", SecurityUtils.getCurrentUserId(), equipmentDto));
        if (ObjectUtil.isNotNull(equipmentDto.getId())) {
            log.error(StrUtil.format("【新增设备失败】操作人id：{}，新增设备实体默认id应该为空，dept：{}", SecurityUtils.getCurrentUserId(), equipmentDto));
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return equipmentService.create(equipmentDto);
    }

    @DeleteMapping
    @ApiOperation("删除设备")
    @PreAuthorize("@smpe.check('equipment:del')")
    public Result<Object> delete(@RequestBody Set<Long> ids){
        log.info(StrUtil.format("【删除设备 /api/equipment】操作人id：{}，被删除设备id集：{}", SecurityUtils.getCurrentUserId(), ids.toString()));
        return equipmentService.delete(ids);
    }

    @PutMapping
    @ApiOperation("修改设备")
    @PreAuthorize("@smpe.check('equipment:edit')")
    public Result<Object> update(@RequestBody EquipmentDto equipmentDto){
        log.info(StrUtil.format("【修改设备 /api/equipment】操作人id：{}，被修改设备id：{}", SecurityUtils.getCurrentUserId(), equipmentDto.getId()));
        return equipmentService.update(equipmentDto);
    }

    @ApiOperation("导出设备数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@smpe.check('equipment:list')")
    public void download(HttpServletResponse response, String equipmentName) throws IOException {
        log.info(StrUtil.format("【查询设备 /api/equipment】操作人id：{}，设备查询条件 equipmentName：{}", SecurityUtils.getCurrentUserId(), equipmentName));
        equipmentService.download(equipmentName,response);
    }
}
